<?php
/**
 * Created by PhpStorm.
 * User: dev
 * Date: 22/11/14
 * Time: 3:50 PM
 */

class Products_Widget_Requirement_Controller extends Base_Controller {

	public $restful = true;

	public function __construct()
	{
		parent::__construct();
		//--Your Code --//

	}

	public function get_add(){

		return View::make('Products::widget.requirement_post');
	}

	public function post_requirement(){
		 
		 
		$rules = array(
				'selectbox-1' => 'required',
				'requirement_details' => 'required',
				'requirement_name' => 'required',
				'requirement_email' => 'required|email'
		);
		 
		$v = Validator::make(Input::all(), $rules);
		 
		if ($v->fails()) {
			$response = array(
					'status' => 'Fail',
					'message' => '<span class="text text-danger">Invalid Input..</span>',
			);
		} else {
			 
			$data = array(
					'category' => trim(Input::get('selectbox-1')),
					'description' => trim(Input::get('requirement_details')),
					'location' => trim(Input::get('selectbox-mylocation5')),
					'name' => trim(Input::get('requirement_name')),
					'email' => Input::get('requirement_email'),
					'phone_no' => Input::get('requirement_contact'),
			);

			
			
			if (DB::table('postrequirement')->insert($data)) {
				 
				$response = array(
						'status' => 'Success',
						'message' => '<span class="text text-info">Thanks! for posting your requirement...</span>',
				);
				 
				//--send mail to Business Owner --//
				 
			} else {
				$response = array(
						'status' => 'Fail',
						'message' => '<span class="text text-danger">Ah! something went wrong..</span>',
				);
			}
		}
		 
		return Response::json($response);
		 
	}

	public function post_lead(){
		 
		$rules = array(
				'lead_name' => 'required',
				'lead_email' => 'required|email',
				'lead_description' => 'required|min:25'
		);
		 
		$v = Validator::make(Input::all(), $rules);
		 
		if ($v->fails()) {
			$response = array(
					'status' => 'Fail',
					'message' => '<span class="text text-danger">Invalid Input..</span>',
			);
		} else {
			 

			$emailTemp = DB::table('emailtemplate')->where('variable','=','send_lead')->first();


			$temTitle = $emailTemp->title;
			$temDesc = $emailTemp->description;

			$webTitle = Settings::get('en','site_title');
			$body = array(
					'website_title' => "<a href='".URL::base()."'>$webTitle</a>",
					'name' => trim(Input::get('lead_name')),
					'category' => trim(Input::get('req_category')),
					'content' => Input::get('lead_description'),
			);
		
			
			$footer = Settings::get('en','mail_smtp_footer');


			$description = Parser::parse_string($temDesc, $body,TRUE);

			$description .= "<p>$footer</p>";
				
			$p = array(
					'from' => trim(Input::get('lead_email')),
					'to' => trim(Input::get('req_email')),
					'sender_name' => trim(Input::get('lead_name')),
					'reply_to' => 'noreply@gmail.com',
					'subject' => trim($temTitle),
					'body' => trim($description),

			);

			Commoncall::mail($p);

			$response = array(
					'status' => 'Success',
					'message' => '<span class="text text-info">Thanks! Message Sent...</span>',
			);
			 
			//--send mail to Business Owner --//
			 

		}
		 
		return Response::json($response);

		 
	}


}